/**
 * Copyright 2016-2018 mobaas.com
 */
package com.mobaas.devops.controller;

import java.util.UUID;

import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.mobaas.devops.PageList;
import com.mobaas.devops.model.Admin;
import com.mobaas.devops.service.AdminService;
import com.mobaas.devops.util.CryptoUtils;

/**
 * 
 * @author billy zhang
 * 
 */
@Controller
@RequestMapping(value = "/sys")
public class SysController extends BaseController {
	
    private static final org.slf4j.Logger logger = LoggerFactory.getLogger(SysController.class);
    
    @Autowired
    private AdminService adminService;

    @RequestMapping(value = "adminList", method = RequestMethod.GET)
    public ModelAndView adminList(
            @RequestParam(value = "keyword", defaultValue = "", required = false) String keyword,
            @RequestParam(value = "pageNum", defaultValue = "1", required = false) int pageNum,
            @RequestParam(value = "pageSize", defaultValue = "10", required = false) int pageSize) {
        ModelAndView model = new ModelAndView();

        PageList<Admin> page = adminService.selectAdminList(keyword, pageNum, pageSize);
        model.addObject("page", page);

        model.addObject("keyword", keyword);

        model.setViewName("sys/adminlist");

        return model;
    }

    @GetMapping(value = "adminAdd")
    public ModelAndView adminAdd() {
        ModelAndView model = new ModelAndView();

        model.setViewName("sys/adminadd");

        return model;
    }

    @PostMapping(value = "adminAdd")
    @ResponseBody
    public String adminAddPost(
            @RequestParam("name") String name,
            @RequestParam("passwd") String passwd,
            @RequestParam("state") int state) {

        int n = adminService.selectAdminCountByName(name);
        if (n == 0) {
            Admin adm = new Admin();
            adm.setUsername(name);
            adm.setPwdSalt(UUID.randomUUID().toString().substring(0, 12));
            adm.setPassword(CryptoUtils.MD5(CryptoUtils.MD5(passwd) + adm.getPwdSalt()));
            adm.setState(state);

            adminService.insertAdmin(adm);

            return "{\"errCode\":0}";
        } else {
            return "{\"errCode\":-1}";
        }
    }

    @GetMapping(value = "adminEdit")
    public ModelAndView adminEdit(
            @RequestParam("adminid") int adminId) {
        ModelAndView model = new ModelAndView();

        Admin adm = adminService.selectAdminById(adminId);
        model.addObject("admin", adm);

        model.setViewName("sys/adminedit");

        return model;
    }

    @PostMapping(value = "adminEdit")
    @ResponseBody
    public String adminEditPost(
            @RequestParam("adminid") int adminId,
            @RequestParam("passwd") String passwd,
            @RequestParam("state") int state) {

        Admin adm = adminService.selectAdminById(adminId);
        if (!"".equals(passwd)) {
            adm.setPassword(CryptoUtils.MD5(CryptoUtils.MD5(passwd) + adm.getPwdSalt()));
        }
        adm.setState(state);

        adminService.updateAdmin(adm);

        return "{\"errCode\":0}";
    }

    @PostMapping(value="adminDelete")
    @ResponseBody
    public String adminDelete(@RequestParam(value="id")int id) {
    	
    		int result=adminService.deleteAdmin(id);
        return "{\"errCode\":" + result + "}";
    }
    
}
